package validator

import (
	"github.com/Ankr-network/kit/mlog"
	"go.uber.org/zap"
	"gopkg.in/go-playground/validator.v9"
	"sync"
)

var (
	globalValidator *validator.Validate
	once            sync.Once
	log             = mlog.Logger("pkg.validator")
)

func Get() *validator.Validate {
	once.Do(func() {
		globalValidator = validator.New()
		if err := globalValidator.RegisterValidation("phone", PhoneValidateFunc); err != nil {
			log.Fatal("validator.RegisterValidation for phone error", zap.Error(err))
		}
		if err := globalValidator.RegisterValidation("password", PasswordValidateFunc); err != nil {
			log.Fatal("validator.RegisterValidation for password error", zap.Error(err))
		}
	})
	return globalValidator
}

func IsValidationError(err error) bool {
	_, ok := err.(validator.ValidationErrors)
	return ok
}
